package cn.ibizlab.odoo.core.odoo_lunch.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ilunch_order;
import cn.ibizlab.odoo.core.odoo_lunch.domain.Lunch_order;
import cn.ibizlab.odoo.core.odoo_lunch.filter.Lunch_orderSearchContext;
import cn.ibizlab.odoo.core.odoo_lunch.service.ILunch_orderService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[午餐订单] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Lunch_orderServiceImpl implements ILunch_orderService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ilunch_orderClientService lunch_orderClientService;

    @Override
    public void updateBatch(List<Lunch_order> lunch_orders, int batchSize) {

        //lunch_orderClientService.updateBatch(lunch_orders,batchSize) ;

    }

    @Override
    public Lunch_order get(Integer id) {
        Ilunch_order clientModel = lunch_orderClientService.createModel();
        clientModel.setId(id);
        lunch_orderClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Lunch_order get(Lunch_order lunch_order) {
        Lunch_order retlunch_order = this.get(lunch_order.getId()) ;
    	BeanCopier copier=BeanCopier.create(retlunch_order.getClass(), lunch_order.getClass(), false);
        copier.copy(retlunch_order, lunch_order, null);
        return lunch_order ;
    }

    @Override
    public boolean create(Lunch_order lunch_order) {
        boolean bOk = false ;
        Ilunch_order clientModel = this.convert2Model(lunch_order,null);
        lunch_orderClientService.create(clientModel) ;
        this.convert2Domain(clientModel,lunch_order);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ilunch_order clientModel = lunch_orderClientService.createModel();
        clientModel.setId(id);
        lunch_orderClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Lunch_order lunch_order) {
        boolean bOk = false ;
        Ilunch_order clientModel = this.convert2Model(lunch_order,null);
        lunch_orderClientService.update(clientModel) ;
        this.convert2Domain(clientModel,lunch_order) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //lunch_orderClientService.removeBatch(lunch_orders) ;

    }

    @Override
    public void createBatch(List<Lunch_order> lunch_orders, int batchSize) {

                        //lunch_orderClientService.createBatch(lunch_orders,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Lunch_order> searchDefault(Lunch_orderSearchContext context) {
        List<Lunch_order> list = new ArrayList<Lunch_order>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_order> clientModelList = lunch_orderClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_order clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_order>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据公司查询
     */
    @Override
    public Page<Lunch_order> selectByOdoo_company(Integer id){
        Lunch_orderSearchContext context = new Lunch_orderSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_order> list = new ArrayList<Lunch_order>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_order> clientModelList = lunch_orderClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_order clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_order>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据币种查询
     */
    @Override
    public Page<Lunch_order> selectByOdoo_currency(Integer id){
        Lunch_orderSearchContext context = new Lunch_orderSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_order> list = new ArrayList<Lunch_order>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_order> clientModelList = lunch_orderClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_order clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_order>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Lunch_order> selectByOdoo_create(Integer id){
        Lunch_orderSearchContext context = new Lunch_orderSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_order> list = new ArrayList<Lunch_order>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_order> clientModelList = lunch_orderClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_order clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_order>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Lunch_order> selectByOdoo_user(Integer id){
        Lunch_orderSearchContext context = new Lunch_orderSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_order> list = new ArrayList<Lunch_order>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_order> clientModelList = lunch_orderClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_order clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_order>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Lunch_order> selectByOdoo_write(Integer id){
        Lunch_orderSearchContext context = new Lunch_orderSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_order> list = new ArrayList<Lunch_order>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_order> clientModelList = lunch_orderClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_order clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_order>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ilunch_order convert2Model(Lunch_order domain , Ilunch_order model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = lunch_orderClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getPrevious_order_widgetDirtyFlag())
            model.setPrevious_order_widget(domain.getPrevious_order_widget());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getOrder_line_idsDirtyFlag())
            model.setOrder_line_ids(domain.getOrder_line_ids());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getBalance_visibleDirtyFlag())
            model.setBalance_visible(domain.getBalance_visible());
         if(domain.getDateDirtyFlag())
            model.setDate(domain.getDate());
         if(domain.getAlertsDirtyFlag())
            model.setAlerts(domain.getAlerts());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCash_move_balanceDirtyFlag())
            model.setCash_move_balance(domain.getCash_move_balance());
         if(domain.getPrevious_order_idsDirtyFlag())
            model.setPrevious_order_ids(domain.getPrevious_order_ids());
         if(domain.getTotalDirtyFlag())
            model.setTotal(domain.getTotal());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCurrency_id_textDirtyFlag())
            model.setCurrency_id_text(domain.getCurrency_id_text());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Lunch_order convert2Domain( Ilunch_order model ,Lunch_order domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Lunch_order();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getPrevious_order_widgetDirtyFlag())
            domain.setPrevious_order_widget(model.getPrevious_order_widget());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getOrder_line_idsDirtyFlag())
            domain.setOrder_line_ids(model.getOrder_line_ids());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getBalance_visibleDirtyFlag())
            domain.setBalance_visible(model.getBalance_visible());
        if(model.getDateDirtyFlag())
            domain.setDate(model.getDate());
        if(model.getAlertsDirtyFlag())
            domain.setAlerts(model.getAlerts());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCash_move_balanceDirtyFlag())
            domain.setCash_move_balance(model.getCash_move_balance());
        if(model.getPrevious_order_idsDirtyFlag())
            domain.setPrevious_order_ids(model.getPrevious_order_ids());
        if(model.getTotalDirtyFlag())
            domain.setTotal(model.getTotal());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCurrency_id_textDirtyFlag())
            domain.setCurrency_id_text(model.getCurrency_id_text());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
